package com.yeqian.user.security;

import com.yeqian.entity.ResultJson;
import com.yeqian.user.entity.LoginConstant;
import org.springframework.security.authentication.AccountExpiredException;
import org.springframework.security.authentication.BadCredentialsException;
import org.springframework.security.authentication.DisabledException;
import org.springframework.security.authentication.LockedException;
import org.springframework.security.core.AuthenticationException;
import org.springframework.security.core.userdetails.UsernameNotFoundException;

/**
 * 登陆失败上下文对象
 */
public class LoginExceptionContext {

    public static ResultJson getLoginException(AuthenticationException exception) {
        ResultJson result;
        if (exception instanceof AccountExpiredException) {
            result = ResultJson.error(2000, LoginConstant.ACCOUNT_EXPIRED);
        } else if (exception instanceof BadCredentialsException) {
            result = ResultJson.error(2001, LoginConstant.WRONG_PASSWORD);
        } else if (exception instanceof DisabledException) {
            result = ResultJson.error(2003, LoginConstant.ACCOUNT_UNAVAILABLE);
        } else if (exception instanceof LockedException) {
            result = ResultJson.error(2004, LoginConstant.ACCOUNT_LOCKOUT);
        } else if (exception instanceof UsernameNotFoundException) {
            result = ResultJson.error(2005, LoginConstant.USER_DOES_NOT_EXIST);
        } else {
            result = ResultJson.error(2100, LoginConstant.OTHER_ERRORS);
        }
        return result;
    }
}
